#!/bin/bash

WORKSPACE="$(pwd)"
export WORKSPACE

# i.e. source and export the variables
export $(cat .ci/java-versions.properties | grep '=' | xargs)

JAVA_HOME="$HOME/.java/$ESH_BUILD_JAVA"
export JAVA_HOME

RUNTIME_JAVA_HOME="$HOME/.java/$ESH_RUNTIME_JAVA"
export RUNTIME_JAVA_HOME

JAVA7_HOME="$HOME/.java/java7"
export JAVA7_HOME

JAVA8_HOME="$HOME/.java/java8"
export JAVA8_HOME

JAVA9_HOME="$HOME/.java/java9"
export JAVA9_HOME

JAVA10_HOME="$HOME/.java/java10"
export JAVA10_HOME

JAVA11_HOME="$HOME/.java/java11"
export JAVA11_HOME

JAVA12_HOME="$HOME/.java/openjdk12"
export JAVA12_HOME

JAVA13_HOME="$HOME/.java/openjdk13"
export JAVA13_HOME

JAVA14_HOME="$HOME/.java/openjdk14"
export JAVA14_HOME

if [[ "${USE_MAVEN_GPG:-}" == "true" ]]; then
  vault_path="kv/ci-shared/release-eng/team-release-secrets/es-delivery/gpg"
  ORG_GRADLE_PROJECT_signingKey=$(vault kv get --field="private_key" $vault_path)
  ORG_GRADLE_PROJECT_signingPassword=$(vault kv get --field="passphase" $vault_path)
  export ORG_GRADLE_PROJECT_signingKey
  export ORG_GRADLE_PROJECT_signingPassword
fi

if [[ "$USE_DRA_CREDENTIALS" == "true" ]]; then
  DRA_VAULT_ROLE_ID_SECRET=$(vault read -field=role-id secret/ci/elastic-elasticsearch-hadoop/legacy-vault-credentials)
  export DRA_VAULT_ROLE_ID_SECRET

  DRA_VAULT_SECRET_ID_SECRET=$(vault read -field=secret-id secret/ci/elastic-elasticsearch-hadoop/legacy-vault-credentials)
  export DRA_VAULT_SECRET_ID_SECRET

  DRA_VAULT_ADDR=https://secrets.elastic.co:8200
  export DRA_VAULT_ADDR
fi

# Initialize the build scan and gobld annotations with empty/open <details> tags
# This ensures that they are collapsible when they get appended to
if [[ "${BUILDKITE_LABEL:-}" == *"Pipeline upload"* || "${BUILDKITE_LABEL:-}" == *"Upload Pipeline"* ]]; then
  cat << EOF | buildkite-agent annotate --context "gradle-build-scans" --style "info"
<details>

<summary>Gradle build scan links</summary>
EOF
fi
